package cn.edu.controller;

import cn.edu.entity.User;
import com.alibaba.fastjson.JSON;
import netscape.javascript.JSObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

//@RestController
@Controller
//@ResponseBody
public class MVCController {



    @RequestMapping("/select.user")
    public String selectAll(){
        return "";
    }


    @RequestMapping("/mvc07")
    public String set07(@RequestParam("name") String username){
        System.out.println(username);
        return null;
    }
    /**
     * Servlet里面控制从页面获取的数据及传输给页面的数据
     * 都会设置其字符集编码.设计一个Filter过滤器.来过滤所有的请求.
     *
     *
     * */


    @RequestMapping(path="/mvc06",produces = "text/html;charset=utf-8")
    @ResponseBody //标明当前返回时,返回的JSON数据
    //对于AJAX标注上当前的ResponseBody.   标注在方法上.
    public String set06(CharacterEncodingFilter characterEncodingFilter){
        characterEncodingFilter.setEncoding("utf-8");

        String str = "Hello World";
        List<User> list  = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            User user = new User();
            user.setId(1);
            user.setName("中文");
            user.setPassword("admin");
            list.add(user);
        }
        String s = JSON.toJSONString(list);
        return s;//返回到页面的字符串
    }




    @RequestMapping("/mvc05")
    public String set05(ModelMap map){
        map.put("msg","ModelMap... args...");
        map.put("msg1","ModelMap... args1...");
        return "mvc03";
    }





    @RequestMapping("/mvc04")
    public void set04(HttpServletRequest request, HttpServletResponse response){
        request.setAttribute("msg","Requerst..Msg..");
        try {
            request.getRequestDispatcher("/front/mvc02.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  ModelAndView
     *      Model
     *      View
     * */
    @RequestMapping("/mvc03")
    public void set03(Model model){
        model.addAttribute("msg1","Model添加的对象");
    }


    @RequestMapping("/mvc02")
    public ModelAndView set02(ModelAndView mav){
        mav.addObject("msg1","Model添加的对象");
        return mav;
    }
    /**
     * ModelAndView是本身在Controller中就存在的对象.
     *  不返回ModelAndView对象,那么就会返回默认的MAV对象.
     * */
    @RequestMapping("/mvc01")
    public ModelAndView set01(){
        ModelAndView mav = new ModelAndView();
        mav.addObject("msg","Model添加的对象");
        return mav;
//        req.setAttribute("msg","");
    }



    @RequestMapping("/mvc")
    public String say(){
        System.out.println("访问到了Controller");
        return "success";//响应给用户去访问的地址.
        /**当方法设定为void,而没有返回值.MVC的内置的视图解析器.会将请求的url
         * 重新发送回响应的内容.
         * 所以+前缀+后缀    /front/   +  mvc  +   .jsp
         * */
        /**
         *  需要指定访问某一个页面,
         *  就直接返回一个当前页面的去除.jsp的字符串,
         *  即可.
         *
         * */
    }

}
